package com.demo.xmtx.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {
  private static final String SECRET_KEY = "secret";

  public static String generateToken(String subject) {
    return Jwts.builder()
        .setSubject(subject)
        .setIssuedAt(new Date())
        .setExpiration(new Date(System.currentTimeMillis() + 86400000)) // 1 day
        .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
        .compact();
  }

  public static String validateToken(String token) {
    return Jwts.parser()
        .setSigningKey(SECRET_KEY)
        .parseClaimsJws(token)
        .getBody()
        .getSubject();
  }


  public static void main(String[] args) {
    String xmtx = generateToken("xmtx");
    System.out.println(xmtx);

    String validateToken = validateToken(xmtx);
    System.out.println(validateToken);
  }
}
